﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Think.Lenovo.Com.DataAccess;
using Think.Lenovo.Com.Models.Material;
using System.Data;
namespace Think.Lenovo.Com
{
    public partial class MaterialManage : System.Web.UI.Page
    {

        MaterialModel model = new MaterialModel();
        MaterialInfo modelInfo = new MaterialInfo();

        #region ClassKind属性
        protected int ClassKind
        {
            get
            {
                return (int)ViewState["ClassKind"];
            }
            set
            {
                ViewState["ClassKind"] = value;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ClassKind = Convert.ToInt32(Request.QueryString["ClassKind"]);
                BindData();
            }
        }

        #region 数据绑定
        private void BindData()
        {

            this.btnAdd.Attributes.Add("onclick", "return ChkInput()");
            this.btnEdit.Attributes.Add("onclick", "return ChkInput()");

            DataSet ds = modelInfo.GetClassList(this.ClassKind);

            this.rptMenuList.DataSource = ds.Tables[0].DefaultView;
            this.rptMenuList.DataBind();

            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("添加为根栏目", "0"));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int ClassTj = Convert.ToInt32(dr["ClassTj"]);
                string ClassId = dr["ClassId"].ToString().Trim();
                string ClassName = dr["ClassName"].ToString().Trim();

                if (ClassTj == 1)
                {
                    this.DdlMenu.Items.Add(new ListItem(ClassName, ClassId));

                }
                else
                {
                    ClassName = "├ " + ClassName;
                    ClassName = StringHelper.StringOfChar(ClassTj - 1, "　") + ClassName;

                    this.DdlMenu.Items.Add(new ListItem(ClassName, ClassId));
                }
            }

        }
        #endregion

        #region 添加按钮操作
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string ClassId = StringHelper.GetRamCode();                   //栏目ID
            string ClassName = this.txtClassName.Text.Trim();               //栏目名称
            string ClassList = "";                                          //栏目包含列表
            string ClassPre = this.DdlMenu.SelectedValue.Trim();            //上一级目录
            int ClassTj = 1;                                                //栏目深度

            if (ClassPre == "0")
            {
                ClassList = ClassId + ",";
                ClassTj = 1;
            }
            else
            {
                DataSet ds = modelInfo.GetClassListByClassId(ClassPre);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    ClassList = dr["ClassList"].ToString().Trim() + ClassId + ",";
                    ClassTj = Convert.ToInt32(dr["ClassTj"]) + 1;
                }
            }
            model.ClassId = ClassId;
            model.ClassName = ClassName;
            model.ClassList = ClassList;
            model.ClassPre = ClassPre;
            model.ClassTj = ClassTj;
            model.ClassKind = this.ClassKind;
            if (modelInfo.ClassAdd(model) > 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('栏目添加成功！')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('栏目添加操作失败！')", true);
            }
            this.txtClassName.Text = "";
            BindData();
        }
        #endregion

        #region 保存栏目信息
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            this.btnEdit.Visible = false;
            this.btnAdd.Visible = true;
            string ClassId = this.HidClassId.Value.Trim();
            string ClassName = this.txtClassName.Text.Trim();
            string ClassList = "";                                            //栏目包含列表
            string ClassPre = this.DdlMenu.SelectedValue.Trim();              //上一级目录
            int ClassTj = 1;                                                  //栏目深度

            if (ClassPre == "0")
            {
                ClassList = ClassId + ",";
                ClassTj = 1;
            }
            else
            {
                DataSet ds = modelInfo.GetClassListByClassId(ClassPre);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    ClassList = dr["ClassList"].ToString().Trim() + ClassId + ",";
                    ClassTj = Convert.ToInt32(dr["ClassTj"]) + 1;
                }
            }
            model.ClassId = ClassId;
            model.ClassName = ClassName;
            model.ClassList = ClassList;
            model.ClassPre = ClassPre;
            model.ClassTj = ClassTj;
            if (modelInfo.ClassSave(model) > 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('栏目保存成功！')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('栏目保存操作失败！')", true);
            }
            this.txtClassName.Text = "";
            BindData();
        }
        #endregion

        #region 显示数据处理
        protected void rptMenuList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
                //HyperLink LabClassNm = (HyperLink)e.Item.FindControl("LabClassNm");
                Label LabClassNm = (Label)e.Item.FindControl("LabClassNm");

                string LitStyle = "<span style=width:{0}px;text-align:right;display:inline-block;>{1}{2}</span>";

                string LitImg1 = "<img src=images/openfolder.gif align=absmiddle />";
                string LitImg2 = "<img src=images/file.gif align=absmiddle />";
                string LitImg3 = "<img src=images/t.gif align=absmiddle />";

                DataRowView drv = (DataRowView)e.Item.DataItem;
                int ClassTj = Convert.ToInt32(drv["ClassTj"]);

                if (ClassTj == 1)
                {
                    LabClassNm.Font.Bold = true;
                    LitFirst.Text = LitImg1;
                }
                else
                {
                    LitFirst.Text = string.Format(LitStyle, ClassTj * 20, LitImg3, LitImg2);
                }
            }
        }
        #endregion

        #region 编辑删除处理
        protected void rptMenuList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField txtClassId = (HiddenField)e.Item.FindControl("txtClassId");
            //HyperLink LabClassNm = (HyperLink)e.Item.FindControl("LabClassNm");
            Label LabClassNm = (Label)e.Item.FindControl("LabClassNm");
            switch (e.CommandName.ToLower())
            {
                case "btnedit":
                    this.txtClassName.Text = LabClassNm.Text.Trim();
                    this.HidClassId.Value = txtClassId.Value.Trim();
                    this.DdlMenu.SelectedValue = modelInfo.GetPreClassId(txtClassId.Value.Trim()).Trim();
                    this.btnAdd.Visible = false;
                    this.btnEdit.Visible = true;
                    break;

                case "btndelete":
                    if (modelInfo.DelByClassId(txtClassId.Value) > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('所选栏目删除成功！')", true);
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('删除操作失败，请与管理员联系！')", true);
                    }
                    BindData();
                    break;
            }
        }
        #endregion

        #region 列表顺序保存操作
        /// <summary>
        /// 列表顺序保存操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            int UpSum = 0;
            bool ReVal = true;
            foreach (RepeaterItem RptItem in this.rptMenuList.Items)
            {
                HiddenField txtClassId = (HiddenField)RptItem.FindControl("txtClassId");
                TextBox txtOrder = (TextBox)RptItem.FindControl("txtOrder");
                try
                {
                    ReVal = modelInfo.UpdateClassOrder(txtClassId.Value.Trim(), Convert.ToInt32(txtOrder.Text));
                }
                catch
                {
                    ReVal = false;
                }

                if (ReVal == false)
                {
                    ++UpSum;
                }
            }
            if (UpSum == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", "alert('所有设定的栏目顺序都已保存成功！')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "ajaxjs", string.Format("alert('栏目顺序部份保存成功,共有{0}条记录保存失败!')", UpSum), true);
            }
            BindData();
        }
        #endregion

        #region
        public static class StringHelper
        {
            /// <summary>
            /// 生成指定长度的字符串,即生成strLong个str字符串
            /// </summary>
            /// <param name="strLong">生成的长度</param>
            /// <param name="str">以str生成字符串</param>
            /// <returns></returns>
            public static string StringOfChar(int strLong, string str)
            {
                string ReturnStr = "";
                for (int i = 0; i < strLong; i++)
                {
                    ReturnStr += str;
                }

                return ReturnStr;
            }

            /// <summary>
            /// 生成日期随机码
            /// </summary>
            /// <returns></returns>
            public static string GetRamCode()
            {
                #region
                return DateTime.Now.ToString("yyyyMMddHHmmssffff");
                #endregion
            }
        }
        #endregion
    }
}